package com.ksyun.campus.metaserver.controller;

import com.ksyun.campus.metaserver.domain.ReplicaData;
import com.ksyun.campus.metaserver.domain.StatInfo;
import com.ksyun.campus.metaserver.services.IFileOperateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController("/")
public class MetaController {

    @Resource
    private IFileOperateService iFileOperateService;

    @RequestMapping(value = "stats", method = RequestMethod.GET)
    public ResponseEntity stats(@RequestHeader String fileSystem, @RequestParam String path) {

        return new ResponseEntity(HttpStatus.OK);
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public ResponseEntity createFile(@RequestHeader String fileSystem, @RequestParam String path, @RequestParam MultipartFile data) {
        // service
        List<ReplicaData> file = iFileOperateService.createFile(fileSystem, path, data);
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * boolean
     * @param fileSystem
     * @param path
     * @return
     */
    @RequestMapping(value = "mkdir", method = RequestMethod.POST)
    public ResponseEntity mkdir(@RequestHeader String fileSystem, @RequestParam String path) {
        return new ResponseEntity(HttpStatus.OK);
    }

    @RequestMapping(value = "listdir", method = RequestMethod.GET)
    public ResponseEntity listdir(@RequestHeader String fileSystem, @RequestParam String path) {


        return new ResponseEntity(HttpStatus.OK);
    }

    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public ResponseEntity delete(@RequestHeader String fileSystem, @RequestParam String path) {
        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * 保存文件写入成功后的元数据信息，包括文件path、size、三副本信息等
     * 提交写
     * @param fileSystem
     * @param path
     * @param offset
     * @param length
     * @return
     */
    @RequestMapping(value = "write", method = RequestMethod.POST)
    public ResponseEntity commitWrite(@RequestHeader String fileSystem, @RequestParam String path, @RequestParam int offset, @RequestParam int length, @RequestParam MultipartFile data) {

        return new ResponseEntity(HttpStatus.OK);
    }

    /**
     * 根据文件path查询三副本的位置，给客户端返回 具体ds、文件分块信息
     *
     * @param fileSystem
     * @param path
     * @return
     */
    @RequestMapping(value = "open", method = RequestMethod.GET)
    public ResponseEntity<StatInfo> open(@RequestHeader String fileSystem, @RequestParam String path) {
        StatInfo fileInfo = iFileOperateService.getFileInfo(fileSystem, path);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("content-type", "application/json");
        return new ResponseEntity<>(fileInfo, httpHeaders, HttpStatus.OK);
    }

    /**
     * 关闭退出进程
     */
    @RequestMapping("shutdown")
    public void shutdownServer() {
        System.exit(-1);
    }

}
